import 'dart:io';

import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:url_launcher/url_launcher_string.dart';

class MapNavigatorUtil {

  /// google地图导航
  static Future<bool> gotoGoogleMap({longitude, latitude, VoidCallback? toInstallCallBack}) {
    final url = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
    return gotoMap(url: url);
  }

  /// 高德地图导航
  static Future<bool> gotoAMap(
      {longitude, latitude, VoidCallback? toInstallCallBack}) {
    var url =
        'amapuri://route/plan/?dlat=$latitude&dlon=$longitude&dev=0&t=3&rideType=elebike'; // 骑行导航路径规划

    if (Platform.isIOS) {
      url =
          'iosamap://path?sourceApplication=amap&dlat=$latitude&dlon=$longitude&dev=0&t=3&rideType=elebike';
    }

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          if (null != toInstallCallBack) {
            toInstallCallBack();
          }
        });
  }

  /// 腾讯地图导航
  static Future<bool> gotoTencentMap(
      {longitude, latitude, VoidCallback? toInstallCallBack}) async {
    var url =
        'qqmap://map/routeplan?type=bike&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          if (null != toInstallCallBack) {
            toInstallCallBack();
          }
        });
  }

  /// 百度地图导航
  static Future<bool> gotoBaiduMap(
      {longitude, latitude, VoidCallback? toInstallCallBack}) async {
    var url =
        "baidumap://map/direction?destination=$latitude,$longitude&coord_type=gcj02&mode=riding";

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          if (null != toInstallCallBack) {
            toInstallCallBack();
          }
        });
  }

  /// 跳转到第三方地图
  /// [url]跳转地址
  /// [toInstallCallBack]地图未安装回调
  static Future<bool> gotoMap(
      {required String url, VoidCallback? toInstallCallBack}) async {
    /// 判断地图是否有安装
    bool canLaunchUrl = await canLaunchUrlString(url);

    if (!canLaunchUrl) {
      EasyLoading.showToast(S.current.openMapFail);
      if (null != toInstallCallBack) {
        toInstallCallBack();
      }

      return false;
    }

    EasyLoading.showToast(S.current.openMapDing);
    await launchUrlString(url);

    return true;
  }

  static void toInstallMap(String url) {
    launchUrlString(url);
  }

  static Future<bool> isBaiduMapInstall() {
    String url = 'baidumap://map/direction';
    return canLaunchUrlString(url);
  }

  static Future<bool> isTencentMapInstall() {
    String url = 'qqmap://map/routeplan';
    return canLaunchUrlString(url);
  }

  static Future<bool> isAmapMapInstall() {
    String url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi';
    return canLaunchUrlString(url);
  }

  static Future<bool> isGoogleMapInstall() {
    String url = 'comgooglemaps://';
    return canLaunchUrlString(url);
  }


  static Future showMapModalPopup(
      BuildContext context, num? longitude, num? latitude) async {
    final color = Theme.of(context).colorScheme.primary;
    bool hasAMap = await MapNavigatorUtil.isAmapMapInstall();
    bool hasBaiduMap = await MapNavigatorUtil.isBaiduMapInstall();
    bool hasTencentMap = await MapNavigatorUtil.isTencentMapInstall();
    bool hasGoogleMap = await MapNavigatorUtil.isGoogleMapInstall();

    final textStyle = TextStyle(
      fontSize: 18,
      fontWeight: FontWeight.w500,
      color: color,
    );

    Widget actionSheet = CupertinoActionSheet(
      title: Text(S.current.trySelect,
          style: TextStyle(
              fontSize: 32.rpx,
              color: Colors.black,
              fontWeight: FontWeight.w500)),
      actions: <Widget>[
        if(hasGoogleMap)
          CupertinoActionSheetAction(
            child: Text(
              S.current.googleMap,
              style: textStyle,
            ),
            onPressed: () {
              MapNavigatorUtil.gotoGoogleMap(longitude: longitude, latitude: latitude);
              // 关闭菜单
              Navigator.of(context).pop();
            },
          ),
        if (hasAMap)
          CupertinoActionSheetAction(
            child: Text(
              S.current.aMap,
              style: textStyle,
            ),
            onPressed: () {
              MapNavigatorUtil.gotoAMap(
                  longitude: longitude, latitude: latitude);
              // 关闭菜单
              Navigator.of(context).pop();
            },
          ),
        if (hasBaiduMap)
          CupertinoActionSheetAction(
            child: Text(
              S.current.baiDuMap,
              style: textStyle,
            ),
            onPressed: () {
              MapNavigatorUtil.gotoBaiduMap(
                  longitude: longitude, latitude: latitude);
              // 关闭菜单
              Navigator.of(context).pop();
            },
          ),
        if (hasTencentMap)
          CupertinoActionSheetAction(
            child: Text(
              S.current.tenXunMap,
              style: textStyle,
            ),
            onPressed: () {
              MapNavigatorUtil.gotoTencentMap(
                  longitude: longitude, latitude: latitude);
              // 关闭菜单
              Navigator.of(context).pop();
            },
          ),
        if (!hasAMap && !hasTencentMap && !hasBaiduMap)
          CupertinoActionSheetAction(
            child: Text(
              S.current.mapNotFound,
              style: textStyle,
            ),
            onPressed: () {
              // 关闭菜单
              Navigator.of(context).pop();
            },
          ),
      ],
      cancelButton: CupertinoActionSheetAction(
        child: Text(
          S.current.cancel,
          style: TextStyle(
            fontSize: 36.rpx,
            fontWeight: FontWeight.w500,
            color: color,
          ),
        ),
        onPressed: () {
          // 关闭菜单
          Navigator.of(context).pop();
        },
      ),
    );

    if (context.mounted) {
      showCupertinoModalPopup(
        context: context,
        builder: (BuildContext context) => actionSheet,
      );
    }
  }
}
